#!/bin/bash

cd /home/qie_cross/code/run

SET_ID=1

red="\033[31m"
green="\033[32m"
yellow="\033[33m"
no="\033[0m"

svr_list=`echo /home/qie_cross/code/run/{crosssvrd,crossdbsvrd,crosslogsvrd}`
cd /home/qie_cross/code/run

rm -rf ./cid/15.cid
rm -rf ./cid/16.cid
rm -rf ./cid/17.cid

rm -rf ./tid/15.tid
rm -rf ./tid/16.tid
rm -rf ./tid/17.tid


if [ ! -s /home/qie_cross/code/run/mon_cross_qie.pid ]
then
   if pgrep -U df -f mon_cross_qie.sh &> /dev/null
   then
        kill -9 `pgrep -f  mon_cross_qie.sh`
      echo echo -e "$green $(date) kill mon process ok $no"
   else
      echo -e "$yellow $(date) the mon process not running $no"
   fi
else
     kill -9 `cat /home/qie_cross/code/run/mon_cross_qie.pid` &> /dev/null  
   sleep 1
   if pgrep -U df  -f mon_cross_qie.sh &> /dev/null
   then
      echo -e "$red $(date) the mon process stop err $no"
   else
      echo -e "$green $(date) kill mon process ok $no"
      rm -f /home/qie_cross/code/run/mon_cross_qie.pid
   fi
fi 

stop()
{
list=$1
for i in $svr_list
do
   cd $i
   j=`echo ${i##*/}` 
   m=`tr -d '[0-9]' <<< $j`
   if [ -s ${m}.pid ]
   then 
      read SVC_PID < ${m}.pid
   else
      read SVC_PID <<< 0
   fi
   if [ -s commsvrd.pid ]
   then
      read com < commsvrd.pid
   else
      read com <<< 0  
   fi

   if [ $SVC_PID = 0 ]
   then
      if pgrep -U df -f  "$m.*$j" &> /dev/null
      then
           kill -9 `pgrep -U df -f  "$m.*$j"`
         echo -e "$green $(date) kill  $j $m ok  $no"
      else
         echo -e "$yellow $(date) $j $m not running $no"
      fi
   else
        kill -3 $SVC_PID &> /dev/null
      while true
      do
        kill -0 $SVC_PID  &> /dev/null ||  break 
      sleep 1
      done
      sleep 1
      if pgrep -U df -f  "$m.*$j" &> /dev/null
      then
           kill -9 `pgrep -U df -f  "$m.*$j"`
         echo -e "$green $(date) kill  $j  $m ok $no"
         rm -f ${m}.pid
      else
         echo -e "$green $(date) SIGQUIT $j  $m ok  $no"
         rm -f ${m}.pid
      fi
   fi

   if [ $com = 0 ]
   then  
      if pgrep -U df -f  "commsvrd.*$j" &> /dev/null
      then
           kill -9 `pgrep -U df -f  "commsvrd.*$j"`
         echo -e "$green $(date) kill  $j commsvrd ok  $no" 
      else
         echo -e "$yellow $(date) $j commsvrd not running $no"
      fi
   else
        kill -3 $com &> /dev/null
      sleep 1
      if pgrep -U df  -f  "commsvrd.*$j" &> /dev/null
      then
           kill -9 `pgrep -U df  -f  "commsvrd.*$j"`
         echo -e "$green $(date) kill  $j  commsvrd ok  $no"
         rm -f commsvrd.pid
      else
         echo -e "$green $(date) SIGQUIT $j commsvrd $no"
         rm -f commsvrd.pid
     fi
   fi
done
}

stop "$svr_list"

mmapdir=`echo /home/mmap/qie_cross`
umount $mmapdir


